/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.report;

import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.jdbc.dao.SmsBeforeProcessedDao;
import com.thienloc.omap.jdbc.dao.SmsBroadcastingDao;
import com.thienloc.omap.jdbc.dao.SmsBroadcastingDao.BroadcastStatistic;
import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class BroadcastEffectivenessBean extends BaseBean implements Serializable {

    private Date startDate;
    private Date endDate;
    private List<BroadcastEffectiveness> list;
    private static final Logger logger = LoggerFactory.getLogger(BroadcastEffectivenessBean.class);

    public void find() {
        SmsBroadcastingDao smsBroadcastingDao = new SmsBroadcastingDao();
        SmsBeforeProcessedDao smsBeforeProcessedDao = new SmsBeforeProcessedDao();
        list = new ArrayList<BroadcastEffectiveness>();
        try {
            List<String> smsList = smsBroadcastingDao.findSmsByDay(startDate);
            List<String> customerList = smsBeforeProcessedDao.findMsisdnByDay(startDate);
            if (smsList != null && !smsList.isEmpty()) {

                for (String sms : smsList) {
                    List<String> broadcastedMsisdnList = smsBroadcastingDao.findMsisdnByDay(sms, startDate);
                    BroadcastStatistic broadcastStatistic = smsBroadcastingDao.getBroadcastStatistic(sms, startDate);
                    broadcastedMsisdnList.retainAll(customerList);

                    BroadcastEffectiveness broadcastEffectiveness = new BroadcastEffectiveness();
                    broadcastEffectiveness.setSms(sms);
                    broadcastEffectiveness.setBroadcastStatistic(broadcastStatistic);
                    broadcastEffectiveness.setMsisdns(broadcastedMsisdnList);
                    list.add(broadcastEffectiveness);
                }

            }
        } catch (Exception ex) {
            String error = "Lỗi! ";
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage(error, ex.getMessage());
        }
    }

    public List<BroadcastEffectiveness> getList() {
        return list;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public class BroadcastEffectiveness {

        private String sms;
        private SmsBroadcastingDao.BroadcastStatistic broadcastStatistic;
        private List<String> msisdns;

        public BroadcastEffectiveness() {
        }

        public List<String> getMsisdns() {
            return msisdns;
        }

        public void setMsisdns(List<String> msisdns) {
            this.msisdns = msisdns;
        }

        public BroadcastStatistic getBroadcastStatistic() {
            return broadcastStatistic;
        }

        public void setBroadcastStatistic(BroadcastStatistic broadcastStatistic) {
            this.broadcastStatistic = broadcastStatistic;
        }

        public String getSms() {
            return sms;
        }

        public void setSms(String sms) {
            this.sms = sms;
        }

        public String getMsisdnList() {
            if (msisdns == null || msisdns.isEmpty()) {
                return "";
            }

            StringBuilder result = new StringBuilder();
            for (String msisdn : msisdns) {
                result.append(msisdn);
                result.append(" ");
            }
            return result.toString();
        }
    }
}
